//
//  DHLoginStyleViewController.m
//  Dresshere
//
//  Created by braspring on 2017/12/19.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHLoginStyleViewController.h"
#import "DHLoginStyleModel.h"
#import "DHStyleCell.h"
#import "DHLoginStyleModel.h"
#import "DHLoginGenderViewController.h"

@interface DHLoginStyleViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;
@property (weak, nonatomic) IBOutlet UIButton *ensureButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) NSMutableArray *selectedArr;

@end

@implementation DHLoginStyleViewController

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (NSMutableArray *)selectedArr{
    if (!_selectedArr) {
        _selectedArr = [NSMutableArray array];
    }
    return _selectedArr;
}

- (void)closeController{
    if (self.userManagerPresent) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [kAppDelegate configHomeController];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (iPhoneX) {
        self.topLayout.constant = 20;
    }
    
    self.ensureButton.layer.cornerRadius = self.ensureButton.height / 2;
    
    [self.ensureButton bk_addEventHandler:^(id sender) {
        if (self.selectedArr.count > 0) {
            NSMutableArray *_idArr = [NSMutableArray array];
            NSMutableArray *styleDic = [NSMutableArray array];
            [self.selectedArr enumerateObjectsUsingBlock:^(DHLoginStyleModel *style, NSUInteger idx, BOOL * _Nonnull stop) {
                [_idArr addObject:style._id];
                [styleDic addObject:[NSDictionary entityToDictionary:style]];
            }];
            [[DHNetAPIClient shareDHNetAPIClient] request_user_stylesWithParams:@{@"styles":_idArr} andBlock:^(id data, NSError *error) {
                [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                    [Login curLoginUser].styles = styleDic;
                    [Login doLogin:[Login curLoginUser]];
                    [self closeController];
                }];
            }];
        }else{
            [NSObject showHudTipStr:@"请选择一个风格"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHStyleCell" bundle:nil] forCellReuseIdentifier:kDHStyleCell];
    
    [self loadData];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"style_selected"]) {
        if ([[userInfo objectForKey:@"isSelected"] boolValue]) {
            [self.selectedArr addObject:[userInfo objectForKey:@"model"]];
        }else{
            [self.selectedArr removeObject:[userInfo objectForKey:@"model"]];
        }
    }
}

- (void)loadData{
    [[DHNetAPIClient shareDHNetAPIClient] request_stylesAndBlock:^(id data, NSError *error) {
        DHLoginStyleList *listModel = [DHLoginStyleList modelWithDictionary:data];
        [self.dataArr removeAllObjects];
        [self.dataArr addObjectsFromArray:listModel.styles];
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHStyleCell forIndexPath:indexPath];
    [cell updateCell:[self.dataArr objectAtIndex:indexPath.row]];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
